<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <Popconfirm
          title="确认删除选中的账号吗?"
          ok-text="确认"
          cancel-text="取消"
          placement="topRight"
          @confirm="handleDeleteMany"
        >
          <a-button type="primary">批量删除</a-button>
        </Popconfirm>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '查看',
              onClick: handleView.bind(null, record),
              color: 'success',
            },
            {
              disabled: record.status === '已下线' ? true : false,
              label: '推荐到首页',
              color: 'warning',
            },
            {
              disabled: record.status === '已下线' ? true : false,
              label: '置顶',
              color: 'warning',
            },
            {
              label: record.status === '已下线' ? '上线' : '下线',
              onClick: handleChangeStatus.bind(null, record),
            },
            {
              label: '删除',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
            {
              label: 'SEO配置',
              onClick: handleSEOConfig.bind(null, record),
            },
            {
              label: '查看评论',
              color: 'warning',
              onClick: handleComment.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>

    <UserModal @register="registerModal" @success="handleSuccess" />
    <SEOModalVue @register="registerSEOConfigModal" @success="handleSuccess" />
    <CommentModalVue @register="registerCommentModal" />
    <ViewModalVue @register="registerViewModal" />
  </div>
</template>

<script lang="ts">
  import { BasicTable, TableAction, useTable } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { defineComponent } from 'vue';
  import UserModal from './personalModal.vue';
  import SEOModalVue from './SEOModal.vue';
  import CommentModalVue from './commentModal.vue';
  import ViewModalVue from './viewModal.vue';
  import { columns, dataSource, searchFormSchema } from './personalManage.data';
  import { message, Popconfirm } from 'ant-design-vue';

  export default defineComponent({
    components: {
      BasicTable,
      UserModal,
      TableAction,
      SEOModalVue,
      CommentModalVue,
      ViewModalVue,
      Popconfirm,
    },
    setup() {
      // 新增或编辑对话框
      const [registerModal /* { openModal } */] = useModal();
      const [registerSEOConfigModal, { openModal: openSEOConfigModal }] = useModal();
      const [registerCommentModal, { openModal: openCommentModal }] = useModal();
      const [registerViewModal, { openModal: openViewModal }] = useModal();

      const [registerTable, { reload, getSelectRowKeys, clearSelectedRowKeys }] = useTable({
        // api: queryList,
        dataSource,
        useSearchForm: true,
        formConfig: {
          labelWidth: 80,
          schemas: searchFormSchema,
          actionColOptions: { span: 4 },
        },
        columns: columns,
        clickToRowSelect: false,
        rowSelection: { type: 'checkbox' },
      });
      // 编辑
      const handleView = (record) => {
        console.log(record);
        // 获取编辑行的数据
        // *
        openViewModal(true, {
          record: JSON.parse(JSON.stringify(record)),
        });
      };
      // 修改状态
      const handleChangeStatus = (record) => {
        record.status = record.status === '已下线' ? '已上线' : '已下线';
      };
      // 单个删除
      const handleDelete = (record) => {
        console.log(record);
      };
      // 多个删除
      const handleDeleteMany = () => {
        const deleteArr = getSelectRowKeys();
        if (deleteArr && deleteArr.length === 0) {
          return message.warn('请选择要删除的数据！');
        } else {
          message.success('删除成功');
          clearSelectedRowKeys();
        }
      };
      // 新增成功
      const handleSuccess = () => {
        reload();
      };

      // SEO配置
      const handleSEOConfig = () => {
        // 查询SEO配置
        const record = {};
        openSEOConfigModal(true, {
          record: JSON.parse(JSON.stringify(record)),
        });
      };

      // 查看评论
      const handleComment = () => {
        openCommentModal(true);
      };

      return {
        registerTable,
        registerModal,
        handleView,
        handleChangeStatus,
        handleDelete,
        handleSuccess,
        handleSEOConfig,
        registerSEOConfigModal,
        registerCommentModal,
        handleComment,
        registerViewModal,
        handleDeleteMany,
      };
    },
  });
</script>

<style scoped lang="less"></style>
